#include "HelpfulData.h"

void seedRand()
{
	srand(time(NULL));
}

bool colliding(FRect r1, FRect r2)
{
	// check veritcal first
	if( (r1.top > r2.top && r1.top < r2.bottom) 
		|| (r1.bottom > r2.top && r1.bottom < r2.bottom) )
	{
		// check horizantally
		if( (r1.left > r2.left && r1.left < r2.right) 
			|| (r1.right > r2.left && r1.right < r2.right) )
		{
			return true;
		}
	}
	return false;
}

float randomFloat(float min, float max)
{
	// multiply by 1000 to increase accuracy since % only works with integers
	float temp = ( rand() % (int)(((max + 1) - min) * 1000.0f) ) + (min * 1000.0f);
	temp /= 1000.0f;
	return temp;
}

int randomInt(int min, int max)
{
	return ( rand() % ((max + 1) - min) ) + min;
}

RECT fRectToRECT(FRect a_rect)
{
	RECT temp;
	temp.top = a_rect.top;
	temp.bottom = a_rect.bottom;
	temp.left = a_rect.left;
	temp.right = a_rect.right;
	return temp;
}

FRect RECTtoFRect(RECT a_rect)
{
	FRect temp;
	temp.top = a_rect.top;
	temp.bottom = a_rect.bottom;
	temp.left = a_rect.left;
	temp.right = a_rect.right;
	return temp;
}

bool colliding(V2DF point, FRect rect)
{
	if( point.x <= rect.right && point.x >= rect.left 
		&& point.y <= rect.bottom && point.y >= rect.top )
		return true;
	return false;
}

// *************************************************************
// ClDwn Class definitions
// *************************************************************
// basic constructor
ClDwn::ClDwn()
{
	// set basic values
	m_newDuration = -1;
	m_duration = m_timePassed = 0.0f;
	m_active = false;
}

// skeleton destructor
ClDwn::~ClDwn() {}

// setup cool down for the given duration
void ClDwn::initialize(float duration)
{
	// make sure timer is reset
	m_newDuration = -1;
	m_active = false;
	m_timePassed = 0.0f;
	m_duration = duration;
}

// if not already active activate
void ClDwn::activate() { m_active = true; }

// de-activate and reset time passed
void ClDwn::deActivate()
{
	m_active = false;
	m_timePassed = 0.0f;
}

// if not active set duration to given duration...
// ... if active save duration and change when done
// if ignore active is true then set duration no matter what
void ClDwn::changeDuration(float duration, bool ignoreActive)
{
	if( ignoreActive || !m_active )
	{
		m_newDuration = -1;
		m_duration = duration;
	}
	else
	{
		m_newDuration = duration;
	}
}

// if cool down is active then update time passed
// if cool down is not active and a newDuration is waiting change duration
void ClDwn::update(float dT)
{
	// check whether on cooldown or not
	if (m_timePassed < m_duration && m_active)
	{
		// increment cooldown timer
		m_timePassed += dT;
	} // check if cooldown is done and needs reseting
	else if (m_timePassed >= m_duration)
	{
		m_timePassed = 0.0f;
		m_active = false;
	}

	// check if duration needs to be augmented
	if( m_newDuration != -1 && !m_active)
	{
		m_duration = m_newDuration;
		// set new duration back to non-active
		m_newDuration = -1;
	}
}
// *************************************************************
